// 作者:wisher
// 时间:2023/6/24
// 说明:360热播接口采集入库及转发

let json = null
let lastgettime = null
const db = uniCloud.database();
exports.main = async (event, context) => {
	const collection = db.collection('configs');
	// const dbCmd = db.command
	let json = (await collection.where({
		name: "swiperJson"
	}).get()).data[0].value
	let lastgettime = (await collection.where({
		name: "swiperLastJsonTime"
	}).get()).data[0].value

	if (new Date().getTime() - lastgettime >= 1000 * 60) {
		const res = await uniCloud.httpclient.request(
			`https://api.web.360kan.com/v1/block?blockid=522&callback=__jp4`, {
				method: 'get',
				headers: {
					"User-Agent": `Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Mobile Safari/537.36 Edg/114.0.1823.43`,
					"Referer": `https://video.360kan.com/`
				},
				dataType: 'text', // 指定返回值为json格式，自动进行parse
			})

		let data = res.data
		if (data.length >= 80) {
			lastgettime = new Date().getTime()
			data = data.replace('__jp4(', "").replace(');', "")
			data = JSON.parse(data)
			if (data.msg == "Success") {
				json = data.data.lists
				await collection.where({
					name: "swiperJson"
				}).update({
					value: json
				})

				await collection.where({
					name: "swiperLastJsonTime"
				}).update({
					value: lastgettime
				})
			}
		}
	}
	return json
};